package com.newtouch.cq.base.starter.business.controller;


import com.newtouch.cq.base.starter.business.entity.SysMenu;
import com.newtouch.cq.base.starter.business.model.dto.MenuTree;
import com.newtouch.cq.base.starter.business.model.dto.SysMenuAddDTO;
import com.newtouch.cq.base.starter.business.model.dto.SysMenuUpdDTO;
import com.newtouch.cq.base.starter.business.model.vo.MenuVO;
import com.newtouch.cq.base.starter.business.model.vo.UserPermissionInfoModel;
import com.newtouch.cq.base.starter.business.service.IBaseSysMenuService;
import com.newtouch.cq.lang.annotation.NoLog;
import com.newtouch.cq.lang.result.MccObjectResult;
import com.newtouch.cq.lang.result.MccResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * <p>
 * 系统菜单表 前端控制器
 * </p>
 *
 * @author Alex
 * @since 2021-08-02
 */
@RestController
@RequestMapping("/sys/menus")
@Api(tags = "菜单相关接口")
public class BaseSysMenuController {

    @Autowired
    private IBaseSysMenuService menuService;


    /**
     * 获取当前用户信息（角色、权限）
     * 并且异步初始化用户部门信息
     * @return 用户名
     */
    @NoLog
    @ApiOperation("获取当前用户信息（角色、权限、菜单）")
    @GetMapping("getUserPermissionInfo")
    public MccObjectResult<UserPermissionInfoModel> getUserPermissionInfo() {
        return menuService.getUserPermissionInfo();
    }

    /**
     * 返回树形菜单集合
     *
     * @return 树形菜单
     */
    @ApiOperation("返回树形菜单集合")
    @GetMapping( "allTree")
    @NoLog
    public MccObjectResult<List<MenuTree>> getTree() {
        return MccObjectResult.success(menuService.getTree());
    }

    /**
     * 新增菜单
     *
     * @param req 菜单信息
     * @return
     */
    @ApiOperation("新增菜单")
    @PostMapping
    @PreAuthorize("hasAuthority('sys_menu_add')")
    public MccResult menu(@RequestBody SysMenuAddDTO req) {
        menuService.add(req);
        return MccResult.SUCCESS;
    }

    /**
     * 通过标识删除菜单
     * @param id
     * @return
     */
    @ApiOperation("通过标识删除菜单")
    @DeleteMapping("{id}")
    @PreAuthorize("hasAuthority('sys_menu_del')")
    @ApiImplicitParam(name = "id", value = "菜单标识", dataTypeClass = Long.class,paramType = "path")
    public MccResult delete(@PathVariable long id) {
        menuService.delete(id);
        return MccResult.SUCCESS;
    }

    /**
     * 通过ID查询菜单的详细信息
     *
     * @param id 菜单ID
     * @return 菜单详细信息
     */
    @NoLog
    @ApiOperation("通过ID查询菜单的详细信息")
    @GetMapping("{id}")
    @ApiImplicitParam(name = "id", value = "菜单标识", dataTypeClass = Long.class,paramType = "path")
    public MccObjectResult<MenuVO> menu(@PathVariable long id) {
        return MccObjectResult.success(menuService.selectById(id));
    }

    /**
     * 菜单编辑
     * @param req
     * @return
     */
    @ApiOperation("菜单编辑")
    @PutMapping("{id}")
    @PreAuthorize("hasAuthority('sys_menu_upd')")
    @ApiImplicitParam(name = "id", value = "菜单标识", dataTypeClass = Long.class,paramType = "path")
    public MccResult update(@PathVariable long id,@RequestBody SysMenuUpdDTO req) {
        menuService.upadte(id,req);
        return MccResult.SUCCESS;
    }

    /**
     * 获取用户权限类的菜单树
     *
     * @return 树形菜单
     */
    @NoLog
    @ApiOperation("获取用户权限类的菜单树")
    @GetMapping(value = "allTreeCurrentUser")
    public MccObjectResult<List<MenuTree>> allTreeCurrentUser() {
        return MccObjectResult.success(menuService.allTreeCurrentUser());
    }

    /**
     * 通过角色标识查询菜单集合
     *
     * @return 树形菜单
     */
    @NoLog
    @ApiOperation("通过角色标识查询菜单集合")
    @GetMapping(value = "/getRolePermissions/{roleId}")
    @ApiImplicitParam(name = "roleId", value = "角色标识", dataTypeClass = Long.class,paramType = "path" )
    public MccObjectResult<List<SysMenu>> getRolePermissions(@PathVariable long roleId) {
        return MccObjectResult.success(menuService.getRolePermissions(roleId));
    }



}
